/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.imenu.web.service;

import com.imenu.ejb.ImenuStoreFacade;
import com.imenu.entity.ImenuStore;
import com.imenu.terminal.TerminalContentServer;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSON;
import net.sf.json.JSONSerializer;

/**
 *
 * @author Yunzi
 */
@WebServlet(name = "TerminalServlet", urlPatterns = {"/terminal"}, loadOnStartup = 1)
public class TerminalServlet extends HttpServlet {

    @Override
    public void init(){
        System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@TerminalServlet.init()");
        try{
            TerminalContentServer.getInstance().start();
        }catch(Exception e){
            e.printStackTrace();
        }
       System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@TerminalServlet.init() done.");
    }
    
    
    @Override
    public void destroy(){
        System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@TerminalServlet.destroy()");
        try{
            TerminalContentServer.getInstance().stop();
        }catch(Exception e){
            e.printStackTrace();
        }
       System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@TerminalServlet.destroy() done.");
    }
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String command = request.getParameter("command");
            System.out.println("TerminalServlet.processRequest() command="+command);
            if ("valid".equals(command)) {
                this.valid(request, response);
            } else if ("course".equals(command)) {
                
            }
        } finally {            
            out.close();
        }
    }
    
    @EJB
    ImenuStoreFacade storeFacade;
    private void valid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String terminalId = request.getParameter("terminalId");
        response.setContentType("application/json");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = response.getWriter();
        try {
            Map jsonResult = new HashMap<String, Object>();
            jsonResult.put("success", true);
            ImenuStore store = storeFacade.findByTerminalId(terminalId);
            System.out.println("TerminalServlet.valid() store =  " + store);
            jsonResult.put("storeId", store.getId());
            out.println(JSONSerializer.toJSON(jsonResult));
        } catch (Exception e) {
            out.println(jsonError(e.getMessage()));
        }
    }
    private JSON jsonError(String message) {
        Map<String, Object> map = new TreeMap<String, Object>();
        map.put("success", false);
        map.put("message", message);
        return (JSONSerializer.toJSON(map));
    }


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
